//
//  RegistrationViewControllerTwo.m
//  结伴
//
//  Created by 莫业金 on 16/8/21.
//  Copyright © 2016年 aleven. All rights reserved.
//

#import "RegistrationViewControllerTwo.h"
#import "RegistrationThreeViewController.h"
#import "ASBirthSelectSheet.h"

@interface RegistrationViewControllerTwo ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *HeadPicture;
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *Birthday;
@property (weak, nonatomic) IBOutlet UITextField *sex;

@end

@implementation RegistrationViewControllerTwo


- (IBAction)headPicture:(UIButton *)sender {
    
    //设置 底部 提示框
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择", nil];
    sheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [sheet showInView:self.view];
    
}


/*
 * 点击了那个按钮的监听事件  (代理方法)
 */
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    
    
    if (buttonIndex == 0) {
        //打开相机
        //资源类型为照相机
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
        //判断是否有相机
        if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            //设置拍照后的图片可被编辑
            picker.allowsEditing = YES;
            //资源类型为照相机
            picker.sourceType = sourceType;
            [self presentViewController:picker animated:YES completion:^(){}];
        }else {
            [MBProgressHUD showError:@"该设备无摄像头"];
        }
        
        
    }else if(buttonIndex == 1){
        //弹出系统的相册
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        //设置相册类型,相册集
        imagePicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        
        imagePicker.delegate = self;
        
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
    
}
#pragma mark - 选择图片完成的时候调用
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeSavedPhotosAlbum) {
        UIImage *icon=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
        //关闭
        [self dismissViewControllerAnimated:YES completion:nil];
        //缩小尺寸
        UIImage *iconimg=[ self imgWithImage:icon];
        //压缩
        NSData *data=UIImageJPEGRepresentation(iconimg, 1);
        UIImage *img=[UIImage imageWithData:data];
        
        [self.HeadPicture setImage:img forState:UIControlStateNormal];

        
        
    }else if (picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        UIImage *icon=[info objectForKey:@"UIImagePickerControllerEditedImage"];
        
        //关闭
        [self dismissViewControllerAnimated:YES completion:nil];
        //缩小尺寸
        UIImage *iconimg=[ self imgWithImage:icon];
        //压缩
        NSData *data=UIImageJPEGRepresentation(iconimg, 1);
        UIImage *img=[UIImage imageWithData:data];
  
        [self.HeadPicture setImage:img forState:UIControlStateNormal];
        
    }
    

    
    [self dismissViewControllerAnimated:YES completion:nil];
}
//MARK:缩小照片的尺寸
-(UIImage *)imgWithImage:(UIImage *)img{
    //获取上下文
    //固定图片宽高
    //    CGFloat w=(Swidth-40)/4;
    //    CGFloat h=50;
    CGFloat w=500;
    CGFloat h=500;
    
    //开启图片上下文
    UIGraphicsBeginImageContext(CGSizeMake(w, h));
    //绘制
    [img drawInRect:CGRectMake(0, 0, w, h)];
    //从上下文获取图片
    UIImage *newImg=UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    return newImg;
}

//图片转字符串 (base64上传图片)
-(NSString *)UIImageToBase64Str:(UIImage *)image
{
    NSData *data = UIImageJPEGRepresentation(image, 1.0f);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    return encodedImageStr;
}




- (IBAction)NextStep:(id)sender {
    

    RegistrationThreeViewController *registration = [[RegistrationThreeViewController alloc] init];
    [self.navigationController pushViewController:registration animated:YES];
    
}
- (IBAction)exit:(id)sender {
    
    [self.view endEditing:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}


- (IBAction)long:(id)sender {
//    
//    [self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -3)] animated:YES];
//    

    [self.navigationController popToRootViewControllerAnimated:YES];
    
}

- (IBAction)BirthdayButton:(UIButton *)sender {
    
     __weak typeof(self) weakSelf = self;
    
    ASBirthSelectSheet *datesheet = [[ASBirthSelectSheet alloc] initWithFrame:self.view.bounds];
    datesheet.selectDate = @"2018-12-12";
    datesheet.GetSelectDate = ^(NSString *dateStr) {
       weakSelf.Birthday.text = dateStr;
    };
    [self.view addSubview:datesheet];
    
}

- (IBAction)six:(id)sender {

    
    [MOAlertView showAlertWithTitle:@"选择性别" message:nil buttonArrayAsString:@[@"男",@"女"] finish:^(NSInteger num) {
        
        if (num == 0) {
            
            self.sex.text = @"男";
            
        }else{
            
            self.sex.text = @"女";
        }
        
    }];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
